import {
  systemList,
  addSystemList,
  EditSystemList,
  bulkSystemDeletion,
  getSystemData
} from '@/api/wcgj'

export default {
  components: {},
  data() {
    return {
      form: {},
      tableData: [],
      addTitle: '',
      addOrEditAccount: 0, //0:添加，1:编辑
      addvisible: false,
      labelPosition: 'right',
      formLabelWidth: '100px',
      queryParam: {
        name: '',
        data: [],
        current: 1,
        total: 1,
        pageSize: 10
      },
      selectionOption: [],
      bulkDisableVisible: false,
      // 验证
      rules: {
        systemName: [
          { required: true, message: '系统名称不能为空', trigger: 'blur' }
        ],
        systemMark: [
          { required: true, message: '系统代号不能为空', trigger: 'blur' }
        ],
        systemIp: [
          { required: true, message: '系统IP地址不能为空', trigger: 'blur' }
        ],
        systemPort: [
          { required: true, message: '系统端口不能为空', trigger: 'blur' }
        ],
        remark: [{ required: false, message: '备注不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  created() {},
  mounted() {
    // this.initTableList()
  },
  methods: {
    // 系统列表数据
    initTableList() {
      systemList({
        pageSize: this.queryParam.pageSize,
        pageCurrent: this.queryParam.current
      })
        .then(res => {
          let data = res.data
          if (data.code == 200) {
            this.tableData = res.data.data.records
            this.queryParam.total = res.data.data.total
          } else {
            this.$message.error(data.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },

    // 添加
    add() {
      this.addTitle = '添加系统'
      this.addOrEditAccount = 0
      for (let key in this.form) {
        this.form[key] = ''
      }
      this.addvisible = true
    },
    // 查询
    makeEnquiries() {
      let param = {
        systemName: this.queryParam.name,
        pageSize: this.queryParam.pageSize,
        pageCurrent: this.queryParam.current
      }
      getSystemData(param).then(res => {
        console.log(res)
        this.tableData = res.data.data.records
      })
    },
    // 添加取消
    closeDialog() {
      this.addvisible = false
    },
    // 添加确定
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.addOrEditAccount == 0) {
            //添加
            addSystemList(this.form)
              .then(res => {
                let data = res.data
                if (data.code == 200) {
                  this.$message.success(data.message)
                  this.addvisible = false
                  this.initTableList()
                } else {
                  this.$message.error(data.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
              })
          } else {
            EditSystemList(this.form)
              .then(res => {
                let data = res.data
                if (data.code == 200) {
                  this.$message.success(data.message)
                  this.addvisible = false
                  this.initTableList()
                } else {
                  this.$message.error(data.message)
                }
              })
              .catch(err => {
                this.$message.error(err.message)
              })
          }
        }
      })
    },

    // 编辑
    handleEdit(row) {
      this.addTitle = '编辑系统'
      this.addOrEditAccount = 1
      this.form = JSON.parse(JSON.stringify(row))
      this.addvisible = true
    },
    // 批量选择
    handleSelectionChange(val) {
      this.selectionOption = val
    },

    // 批量删除
    // bulkDeletion(){
    //   console.log(this.selectionOption)
    //   if(this.selectionOption.length == 0){
    //     return this.$message.warning("请选择删除的文件")
    //   }
    //   this.bulkDisableVisible = true
    // },

    // 删除取消
    cancel() {
      this.bulkDisableVisible = false
    },
    // 删除确定
    bulkDisableSave() {
      bulkSystemDeletion({ ids: this.ids, id: 0 })
        .then(res => {
          console.log(res)
          let data = res.data
          if (data.code == 200) {
            this.bulkDisableVisible = false
            this.$message.success(data.message)
            this.initTableList()
          } else {
            this.$message.error(data.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    // 删除
    handleDelete(row) {
      this.ids = [row.id]
      this.bulkDisableVisible = true
    },
    //分页
    handleSizeChange(val) {
      this.queryParam.pageSize = val
      this.queryParam.pageSize = 1
    },
    handleCurrentChange(val) {
      this.queryParam.current = val
      this.initTableList()
    }
  }
}
